//
//
/////////////////////////////////////////////////////////////////////////////

using FreeSpace.Logic;
using FreeSpace.Scenes;
using FreeSpaceLibrary;
using FreeSpaceLibrary.XnaContentObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

/////////////////////////////////////////////////////////////////////////////
namespace FreeSpace.Core
{
    /////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class GameCore : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        private SceneManager _sceneManager = new SceneManager();
        private MenuScene _menuScene;
        private static FpsCounter _fpsCounter = new FpsCounter();

        /////////////////////////////////////////////////////////////////////////
        public GameCore()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            
            //_graphics.IsFullScreen = true;
        }

        /////////////////////////////////////////////////////////////////////////
        public static FpsCounter FpsCounter
        {
            get { return _fpsCounter; }
        }

        /////////////////////////////////////////////////////////////////////////
        protected override void Initialize()
        {
            base.Initialize();
        }

        /////////////////////////////////////////////////////////////////////////
        protected override void LoadContent()
        {
            GraphicsDevice.Viewport = new Viewport(0, 0, Constants.ScreenWidth, Constants.ScreenHeight);
            //GraphicsDevice.Viewport.Width = Constants.ScreenWidth;
            //GraphicsDevice.Viewport.Height = Constants.ScreenHeight;

            _spriteBatch = new SpriteBatch(GraphicsDevice);
            //ObjectData[] ddd = ContentLoader.GetTargets();

            ContentLoader.Load(Content);

            LoadScene loadScene = new LoadScene(_spriteBatch, GraphicsDevice, Content);
            _sceneManager = new SceneManager();
            _sceneManager.Add(loadScene);
            _sceneManager.Add(new MenuScene(_spriteBatch));
            _sceneManager.Add(new GameScene(_spriteBatch));

            loadScene.Start();
        }

        /////////////////////////////////////////////////////////////////////////
        protected override void UnloadContent()
        {
            
        }

        /////////////////////////////////////////////////////////////////////////
        protected override void Update(GameTime gameTime)
        {
            FpsCounter.Update(gameTime);

            //GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
            foreach (BaseScene scene in _sceneManager.Scenes)
            {
                scene.Update(gameTime, Keyboard.GetState());
            }
            base.Update(gameTime);

        }

        /////////////////////////////////////////////////////////////////////////
        protected override void Draw(GameTime gameTime)
        {
            FpsCounter.Draw(gameTime);

            GraphicsDevice.Clear(Color.Black);

            foreach (BaseScene scene in _sceneManager.Scenes)
            {
                scene.Draw();
            }
            base.Draw(gameTime);

#if DEBUG
            SpriteFont fontSprite = ContentLoader.ContentFonts[FontEnum.MenuItem];
            _spriteBatch.Begin();
            _spriteBatch.DrawString(fontSprite, "ups: " + FpsCounter.Ups, new Vector2(10, 10), new Color(255, 255, 255, 128));
            _spriteBatch.DrawString(fontSprite, "dps: " + FpsCounter.Dps, new Vector2(10, 25), new Color(255, 255, 255, 128));
            _spriteBatch.End();
#endif
        }
    }
/////////////////////////////////////////////////////////////////////////////
}//FreeSpace
